/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

/**
 * A new custom component used to display a check box with an editable, one line
 * text caption, using a text field instead of a check box label. This component
 * is made for convenience, to make it possible for a task to be editable
 * in-place, also, the user expects that he will be able to cut/copy/paste
 * a displayed task's text. This is not possible when using the label of a
 * check box. This component does not fulfill the requirement of custom painting,
 * but this is used in other classes ({@code ImagePanel}),
 * so we expect this component will be accepted as fulfilling the custom
 * component requirement.
 *
 * @author
 * Nosheen Zaza      
 */
public class CheckTextField extends JPanel {

    private JTextField textfield;
    private JCheckBox checkBox;

    /**
     * Creates a new instance, with an empty text, and unchecked text box.
     */
    public CheckTextField() {
        initComponents("", false);
        init();
    }

    /**
     * Creates a new instance, with the given text and status.
     * @param text
     * @param status
     */
    public CheckTextField(String text, boolean status) {
        initComponents(text, status);
        textfield.setText(text);
        init();
    }

    /**
     * Initializes the checkbox and text area.
     * @param text
     * @param status
     */
    private void initComponents(String text, boolean status) {
        // Let the text field have no borders.
        textfield = new JTextField(text){
            @Override public void setBorder(Border border) {
            }
        };
        
        // Make the text field transparent.
        textfield.setOpaque(false);

        // Same for chackbox
        checkBox = new JCheckBox();
        checkBox.setOpaque(false);

        checkBox.setSelected(status);
    }

    /**
     * Initializes {@code CheckTextField}
     * @param id
     */
    private void init() {
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        setOpaque(false);
        add(checkBox);
        add(textfield);
    }

    /**
     * Returns the text field.
     * @return textfield
     */
    public JTextField getTextfield() {
        return textfield;
    }

    /** 
     * Sets the text field.
     * @param textfield
     */
    public void setTextfield(JTextField textfield) {
        this.textfield = textfield;
    }

    /**
     * Returns the checkbox component.
     * @return {@code checkbox}
     */
    public JCheckBox getCheckBox() {
        return checkBox;
    }

    /**
     * Sets the checkbox component to the one given.
     * @param checkBox
     */
    public void setCheckBox(JCheckBox checkBox) {
        this.checkBox = checkBox;
    }
}